#region Imports
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using InsightAnimals = PHSRAG.Insight21.Humans;
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
    /// <summary>
    ///  Navigation Bar for Animals Overview Pages
    /// </summary>
    [ToolboxData("<{0}:AnimalsSubNavigation runat=server />")]
    [Designer(typeof(AnimalsSubNavigationDesigner), typeof(IDesigner))]
    public class AnimalsSubNavigation : SubNavigation
    {
        #region Constants
        private const string selectedAnimalsSubNavigationLinkKey =
            "SelectedAnimalsSubNavigationLink_F8FB1B82-9D06-48f9-B2AC-540F7448F4EF";
        #endregion

        #region Instance Variables
        private LinkButton activeProtocolsLinkButton;
        private LinkButton pendingApplicationsLinkButton;
        private LinkButton workInProgressLinkButton;
        private LinkButton inactiveProtocolsLinkButton;
        private LinkButton wfActivityListLinkButton;
        private LinkButton tissueOnlyLinkButton;
        #endregion

        #region Implementations for abstract properties and methods from Base Class SubNavigation

        /// <summary>
        /// Create all controls in the AnimalsSubNavigation.
        /// </summary>
        protected override void CreateControls()
        {
            Controls.Clear();

            activeProtocolsLinkButton = new LinkButton();
            activeProtocolsLinkButton.CommandName = activeProtocolsLinkButton.Text = "Active Protocols";
            activeProtocolsLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Humans.ActiveProtocolsURL", "");

            pendingApplicationsLinkButton = new LinkButton();
            pendingApplicationsLinkButton.CommandName = pendingApplicationsLinkButton.Text = "Pending Applications";
            pendingApplicationsLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Humans.PendingApplicationsURL", "");

            workInProgressLinkButton = new LinkButton();
            workInProgressLinkButton.CommandName = workInProgressLinkButton.Text = "Work In Progress";
            workInProgressLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Humans.WorkInProgressURL", "");

            inactiveProtocolsLinkButton = new LinkButton();
            inactiveProtocolsLinkButton.CommandName = inactiveProtocolsLinkButton.Text = "Inactive Protocols";
            inactiveProtocolsLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Humans.InactiveProtocolsURL", "");

            tissueOnlyLinkButton = new LinkButton();
            tissueOnlyLinkButton.CommandName = tissueOnlyLinkButton.Text = "Tissue Only";
            tissueOnlyLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Humans.ExemptProtocolsURL", "");

            wfActivityListLinkButton = new LinkButton();
            wfActivityListLinkButton.CommandName = wfActivityListLinkButton.Text = "Activity List";
            wfActivityListLinkButton.CommandArgument = Utility.Settings.GetKeyValue("SubNavigation.Humans.WorkFlowActivityListURL", "");

        }

        /// <summary>
        /// Setup the control tree to reflect the actual (and exact) layout of the controls 
        /// within the AnimalsSubNavigation.
        /// </summary>
        protected override void GenerateControlTree()
        {
            Table table = new Table();
            Controls.Add(table);
            table.CssClass = "SubModuleSummaryNavigation";
            table.EnableViewState = false;

            TableRow row = new TableRow();
            table.Rows.Add(row);

            TableCell cell = new TableCell();
            row.Cells.Add(cell);
            cell.CssClass = "SubModuleSummaryNavigationFillerCell";
            cell.Controls.Add(new LiteralControl("&nbsp;"));

            cell = new TableCell();
            row.Cells.Add(cell);

            table = new Table();
            cell.Controls.Add(table);

            row = new TableRow();
            table.Rows.Add(row);

            cell = new TableCell();
            row.Cells.Add(cell);

            cell = new TableCell();
            row.Cells.Add(cell);
            cell.Controls.Add(activeProtocolsLinkButton);

            cell = new TableCell();
            row.Cells.Add(cell);
            cell.Controls.Add(pendingApplicationsLinkButton);

            cell = new TableCell();
            row.Cells.Add(cell);
            cell.Controls.Add(workInProgressLinkButton);

            cell = new TableCell();
            row.Cells.Add(cell);
            cell.Controls.Add(inactiveProtocolsLinkButton);

            cell = new TableCell();
            row.Cells.Add(cell);
            cell.Controls.Add(tissueOnlyLinkButton);

            cell = new TableCell();
            row.Cells.Add(cell);
            cell.Controls.Add(wfActivityListLinkButton);

            CreateSelectedLinkAdornments(row);
        }

        /// <summary>
        /// The selected link cell is given a prefix and suffix image as adornments. In addition the background of the
        /// cell is of a different style.
        /// </summary>
        private void CreateSelectedLinkAdornments(TableRow row)
        {
            LinkButton selectedLink = GetSelectedLink();
            if (selectedLink != null)
            {
                foreach (TableCell cell in row.Cells)
                {
                    if ((cell.Controls.Count == 1) && (cell.Controls[0] == selectedLink))
                    {
                        cell.CssClass = "SubModuleNavigationSelectedSummary";

                        TableCell adornmentCell = new TableCell();
                        adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
                        row.Cells.AddAt(row.Cells.GetCellIndex(cell), adornmentCell);

                        const string prefixImage = "../Images/Row2_Left.gif";
                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(prefixImage));
                        Image image = new Image();
                        image.ImageUrl = prefixImage;
                        adornmentCell.Width = bitmap.Width;
                        adornmentCell.Controls.Add(image);

                        adornmentCell = new TableCell();
                        adornmentCell.CssClass = "SubModuleNavigationSelectedSummaryAdornment";
                        row.Cells.AddAt(row.Cells.GetCellIndex(cell) + 1, adornmentCell);

                        const string suffixImage = "../Images/Row2_Right.gif";
                        bitmap = new System.Drawing.Bitmap(Context.Server.MapPath(suffixImage));
                        image = new System.Web.UI.WebControls.Image();
                        image.ImageUrl = suffixImage;
                        adornmentCell.Width = bitmap.Width;
                        adornmentCell.Controls.Add(image);

                        break;
                    }
                }
            }
        }

        /// <summary>
        /// Retrieve the name of the link that was last clicked on.
        /// </summary>
        /// <returns>Reference to the link button corresponding to the clicked link</returns>
        protected override LinkButton GetSelectedLink()
        {
            // Anytime a link is clicked on, the OnBubbleEvent handler stores the name of the link 
            // in the session. Retrieve that name and return the corresponding link button reference.

            object o = Page.Session[SelectedSubNavigationLinkKey];
            if (o != null)
            {
                string commandName = (string)o;
                if (commandName == activeProtocolsLinkButton.CommandName)
                {
                    return activeProtocolsLinkButton;
                }
                if (commandName == pendingApplicationsLinkButton.CommandName)
                {
                    return pendingApplicationsLinkButton;
                }
                if (commandName == workInProgressLinkButton.CommandName)
                {
                    return workInProgressLinkButton;
                }
                if (commandName == inactiveProtocolsLinkButton.CommandName)
                {
                    return inactiveProtocolsLinkButton;
                }
                if (commandName == tissueOnlyLinkButton.CommandName)
                    return tissueOnlyLinkButton;
                if (commandName == wfActivityListLinkButton.CommandName)
                {
                    return wfActivityListLinkButton;
                }
            }
            return activeProtocolsLinkButton;
        }

        /// <summary>
        /// 
        /// </summary>
        protected override string SelectedSubNavigationLinkKey
        {
            get
            {
                return selectedAnimalsSubNavigationLinkKey;
            }
        }
        /// <summary>
        /// Set the selected link so that on load, the appropriate tab may be shown highlighted. This property is invoked
        /// by pages while vectoring to other pages which host this navigation control.
        /// </summary>
        public string SelectedLink
        {
            set { Page.Session[SelectedSubNavigationLinkKey] = value; }
        }
        #endregion
    }

    #region Class AnimalsSubNavigationDesigner
    /// <summary>
    /// Designer associated with the control.
    /// </summary>
    public class AnimalsSubNavigationDesigner : ControlDesigner
    {
        #region Overrides
        /// <summary>
        /// Force the control to create its child controls for display at design time.
        /// </summary>
        /// <returns>HTML representation of the control</returns>
        public override string GetDesignTimeHtml()
        {
            ((AnimalsSubNavigation)Component).Design();
            return base.GetDesignTimeHtml();
        }
        #endregion
    }
    #endregion
}
